<template>
	<view>
		<view class="hotpack">			
			<view class="mf-hot-two mf-hot-wrap">
				<view class="mf-hot-two-top" v-for="(item,index) in ticketList" :key="index">
					<view class="mf-hot-two-top-left">
						<view class="mf-hot-two-top-price">
							<text>{{ item.name }}</text>
						</view>
						<view class="mf-hot-two-top-date">{{ timestampToTime(item.start_time) }}-{{ timestampToTime(item.end_time) }}</view>
						<view class="mf-hot-two-top-progress">适用：{{ item.des }}</view>
					</view>
					<view class="mf-hot-two-top-right">
						<view class="mf-hot-two-btn" @click="had_pull(item.coupon_id)">{{ item.is_had ? '已领取' : '立即领取' }}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var app = getApp()
	export default {
		data() {
			return {
				backKey:'mf_ticket',
				uid:'',
				ticket:'',
				ticketList:[],
			}
		},
		methods: {
			timestampToTime(timestamp) {
			    const date = new Date(timestamp * 1000);
			    const Y = date.getFullYear() + '-';
			    const M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
			    const D = date.getDate() + ' ';
			    return Y+M+D;
			},
			had_pull(id){
				const _this = this
				const uid = this.uid
				const _ticket = this.ticket
				if(!uid){
					this.checkLogin()
					return
				}
				uni.request({
					url:'https://www.jlmkkj.com/appapi.php?c=Shop&a=had_pull',
					method:'POST',
					data:{
						'Device-Id': "packapp",
						app_type: "packapp",
						app_version: 1510,
						coupon_id: id,
						now_city: "1574",
						now_lang: "",
						ticket: _ticket,
						wxapp_type: "plat",
					},
					success:res=>{
						uni.showToast({
						    title: '领取成功',
						    duration: 2000
						});
						const ticketList = _this.ticketList
						const _ticketList = ticketList.map(item=>{
							if(item.coupon_id===id){
								item.is_had = true
							}
							return item
						})
						console.log('ticket_list')
						console.log(_ticketList)
						_this.ticketList = _ticketList
					}
				})
			},
			initCheckLogin(){
				const _this = this
				const isLogin = app.globalData.is_login
				if(isLogin){
					const uid = app.globalData.userInfo.uid
					this.uid = uid
					uni.getStorage({
					    key: 'ticket',
					    success: res=> {
					        console.log('ticket:'+ res.data);
							_this.ticket = res.data
					    }
					});
					
				}
			},
			checkLogin(){
				const isLogin = app.globalData.is_login
				const _this = this
				if(!isLogin){
					location.href = 'https://www.jlmkkj.com/packapp/my/login.html?back=' + this.backKey
				}else {
					// 设置用户信息
					const uid = app.globalData.userInfo.uid
					// console.log(uid)
					this.uid = uid
					uni.getStorage({
					    key: 'ticket',
					    success: res=> {
					        console.log('ticket:'+ res.data);
							_this.ticket = res.data
					    }
					});
					
					
				}	
			},
			getTicket(){
				let _uid = this.uid
				let _this = this
				uni.request({
					url:'https://www.jlmkkj.com/appapi.php?c=Mf&a=getRKLTicket',
					success:res=>{
						const lists = res.data.result
						_this.ticketList = lists
					}
				})
			}
		},
		mounted(){
			this.initCheckLogin()
			this.getTicket()
		}
	}
</script>

<style>
	@import url("./hotpack.scss");
</style>
